package com.sx.micowell.module.login

import android.annotation.SuppressLint
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.mvvm.base.data.DataBindingConfig
import com.example.mvvm.until.ActivityManager
import com.example.mvvm.until.LogUtils
import com.gyf.immersionbar.ImmersionBar
import com.sx.micowell.BR
import com.sx.micowell.R
import com.sx.micowell.databinding.ActVerificationCodeBinding
import com.sx.micowell.framework.app.IntentKey
import com.sx.micowell.framework.app.MyActivity
import com.sx.micowell.framework.arouth.RouteManager
import com.sx.micowell.framework.arouth.RoutePath
import com.sx.micowell.framework.model.LoginModel
import com.sx.micowell.framework.model.VerifyCodeModel
import com.tuo.customview.VerificationCodeView
import com.tuo.customview.VerificationCodeView.InputCompleteListener

/**
 * @author: YY
 * @time: 2021/8/3
 * @describe 输入验证码
 */
@Route(path = RoutePath.VERIFY_CODE)
class VerifyCodeActivity : MyActivity<ActVerificationCodeBinding>() {

    @Autowired(name = IntentKey.TYPE)
    @JvmField
    var type: Int = 0

    @Autowired(name = IntentKey.ID)
    @JvmField
    var openId: String = ""

    @Autowired(name = IntentKey.DATA)
    @JvmField
    var phoneNum: String = ""


    lateinit var viewModel: VerifyCodeModel

    override fun initViewModel() {
        super.initViewModel()
        viewModel = getActivityScopeViewModel(VerifyCodeModel::class.java)
    }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.act_verification_code, BR.viewModel, viewModel)
    }

    override fun initView() {
        ImmersionBar.with(this).statusBarDarkFont(true)
            .fitsSystemWindows(true).init()

    }

    @SuppressLint("SetTextI18n")
    override fun initData() {
        if (type == -1) {
            binding.tvNext.text = "确定"
        }
        binding.tvContentPhone.text = "已发送4位验证码至 $phoneNum"
        viewModel.setPhoneNumber(phoneNum, type, openId)
        binding.tvCountDown.start()
        binding.icv.setInputCompleteListener(object : InputCompleteListener {
            override fun inputComplete() {
                LogUtils.d("icv_input ${binding.icv.inputContent}")
                viewModel.setVerifyCode(binding.icv.inputContent)
            }

            override fun deleteContent() {
                LogUtils.d("icv_input, ${binding.icv.inputContent}")
                viewModel.setVerifyCode(binding.icv.inputContent)
            }
        })

    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.sendCodeEvent.observe(this, {
            binding.tvCountDown.start()
        })

        viewModel.bindSuccessEvent.observe(this, {
            LogUtils.d("intentToMain")
            RouteManager.getInstance().intentToMain()
            //todo
            ActivityManager.getInstance().ExitActivity(LoginActivity::class.java)
            finish()
        })
    }

}